#!/bin/sh
#================================================================
#  Description: Send IP info to Flask backend
#  Author: Liu Hanxu
#  Date: 2025-05-01
#================================================================

# 获取当前服务器信息

# 获取IP
ip_address=$(hostname -I | awk '{print $1}')
#echo "当前服务器IP：$ip_address"

# 获取架构信息
arch=$(uname -m)

# 方法一：优先通过/proc/cpuinfo获取CPU型号
get_cpu_by_proc() {
    if [ -r /proc/cpuinfo ]; then
        # 提取首个model name字段并去除首部空格
        grep -m1 "model name" /proc/cpuinfo | awk -F ': ' '{print $2}' | sed 's/^[[:space:]]*//'
    else
        echo ""
    fi
}

# 方法二：回退使用lscpu命令获取
get_cpu_by_lscpu() {
    if command -v lscpu &> /dev/null; then
        # 提取Model name字段并格式化
        lscpu | grep "Model name" | awk -F ': ' '{print $2}' | sed 's/^[[:space:]]*//'
    else
        echo ""
    fi
}

cpu_model=$(get_cpu_by_proc)

if [ -z "$cpu_model" ]; then
    cpu_model=$(get_cpu_by_lscpu)
fi

# 根据CPU型号匹配机器类型
if echo "$cpu_model" | grep -qF "WX-H8000"; then
    server_type="8A服务器"
elif echo "$cpu_model" | grep -qF "SW3231"; then
    server_type="6B服务器"
elif echo "$cpu_model" | grep -qF "SW1621"; then
    server_type="6A服务器"
elif echo "$cpu_model" | grep -qF "Intel"; then
    server_type="X86服务器"
elif lscpu | grep -q "Kunpeng-920"; then
    server_type="鲲鹏920服务器"
else
    server_type="未知类型"
fi

# 输出结果
if [ -n "$cpu_model" ]; then
    echo "CPU型号: $cpu_model"
    echo "机器类型: $server_type"
else
    echo "无法获取CPU型号" >&2
    exit 1
fi

# 获取内核版本
kernel_version=$(uname -r | awk -F'-' '{print $1}')
#echo "当前服务器内核版本：$kernel_version"

# 获取OS版本
os_info=$(grep PRETTY_NAME /etc/os-release | cut -d'"' -f2)
case "$os_info" in
    "CentOS Linux 7 (Core)")
      os="CentOS-7";;
    "CentOS Linux 8 (Core)")
      os="CentOS-8";;
    "UnionTech OS Server 20 Enterprise" | "Uniontech OS Server 20 Enterprise" | "UnionTech OS Server 20")
      os="UOS-20";;
    "openEuler 22.03 (LTS-Next)")
      os="SWOS";;
    *)
      echo "无法处理OS版本" >&2
      ;;
esac

#echo "当前服务器操作系统：$os"

# 获取编译器版本
compiler_version=$(gcc --version | head -n1 | awk '{print $3}')
#echo "当前服务器编译器版本：$compiler_version"

# 手动设置机器状态，在线为true，离线为false
status="true"

# 手动填写摆放位置
location="13楼"

# 手动填写资产编号
#number="XJ-JSJ-xxx-xxxx"
number=""
if [ -z "$number" ]; then
    echo "Error: 资产编号为空, 请手动设置!"
    exit 1
fi

# 手动填写备注
note=""

echo "========================================"
echo "IP      : $ip_address"
echo "机器类型: $server_type"
echo "状态    : $status"
echo "架构    : $arch"
echo "操作系统: $os"
echo "编译器  : $compiler_version"
echo "内核    : $kernel_version"
echo "摆放位置: $location"
echo "资产编号: $number"
echo "备注    : $note"
echo "========================================"


# 发送POST请求
curl -X POST -H "Content-Type: application/json" -d '{
"ip_address": "'"$ip_address"'",
"server_type": "'"$server_type"'",
"status": '"$status"',
"arch": "'"$arch"'",
"os": "'"$os"'",
"compiler": "'"$compiler_version"'",
"kernel": "'"$kernel_version"'",
"location": "'"$location"'",
"number": "'"$number"'",
"note": "'"$note"'"
}' http://172.16.129.102:5000/api/report

# 针对离线或外网的服务器，手动填写服务器信息，并模拟发送POST请求
#curl -X POST -H "Content-Type: application/json" -d '{
#"ip_address": "192.168.4.21",
#"server_type": "鲲鹏920",
#"status": false,
#"arch": "aarch64",
#"os": "openEuler-2403",
#"compiler": "12.3.0",
#"kernel": "6.6",
#"location": "9楼",
#"number": "XJ-JSJ-xxx-xxxx",
#"note": "互联网"
#}' http://172.16.129.102:5000/api/report
